package com.wwc.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwc.common.exception.BusinessException;
import com.wwc.common.exception.BusinessExceptionEnum;
import com.wwc.common.utils.JwtUtil;
import com.wwc.common.utils.SnowUtil;
import com.wwc.member.controller.dto.member.MemberLoginDTO;
import com.wwc.member.controller.dto.member.MemberRegisterDTO;
import com.wwc.member.controller.dto.member.MemberSendCodeDTO;
import com.wwc.member.controller.vo.MemberLoginVO;
import com.wwc.member.entity.Member;
import com.wwc.member.mapper.MemberMapper;
import com.wwc.member.service.IMemberService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService{
	private final static Logger LOG = LoggerFactory.getLogger(MemberServiceImpl.class);

	@Resource
	private MemberMapper memberMapper;

	@Override
	public Long register(MemberRegisterDTO memberRegisterDTO){
		// 根据查询条件获取会员列表
		QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
		memberQueryWrapper.eq("mobile", memberRegisterDTO.getMobile());
		List<Member> memberList = memberMapper.selectList(memberQueryWrapper);

		// 如果手机号已被注册则抛出异常
		if(CollUtil.isNotEmpty(memberList)){
			throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
		}

		// 新增会员
		Member member = new Member();
		member.setId(SnowUtil.getSnowflakeNextId());
		member.setMobile(memberRegisterDTO.getMobile());
		memberMapper.insert(member);
		return member.getId();
	}

	@Override
	public void sendCode(MemberSendCodeDTO memberSendCodeDTO){
		// 根据查询条件获取会员列表
		QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
		memberQueryWrapper.eq("mobile", memberSendCodeDTO.getMobile());
		List<Member> memberList = memberMapper.selectList(memberQueryWrapper);

		// 如果手机号未注册则自动注册
		if(CollUtil.isEmpty(memberList)){
			// 新增会员
			Member member = new Member();
			member.setId(SnowUtil.getSnowflakeNextId());
			member.setMobile(memberSendCodeDTO.getMobile());
			memberMapper.insert(member);
		}

		// 生成短信验证码
		// String sc = RandomUtil.randomString(6);
		String sc = "888888";
		LOG.info("验证码: {}", sc);
	}

	@Override
	public MemberLoginVO login(MemberLoginDTO memberLoginDTO){
		// 根据查询条件获取会员列表
		QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
		memberQueryWrapper.eq("mobile", memberLoginDTO.getMobile());
		List<Member> memberList = memberMapper.selectList(memberQueryWrapper);

		// 如果手机号不存在则抛出异常
		if(CollUtil.isEmpty(memberList)){
			throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
		}

		// 校验验证码，测试环境默认：888888
		if(!"888888".equals(memberLoginDTO.getCode())){
			throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
		}

		// 登陆成功后，返回用户信息和token
		MemberLoginVO memberLoginVO = BeanUtil.copyProperties(memberList.get(0), MemberLoginVO.class);
		String token = JwtUtil.createToken(memberLoginVO.getId(), memberLoginVO.getMobile());
		memberLoginVO.setToken(token);
		return memberLoginVO;
	}
}
